using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using SportTracksPluginFramework;
using ZoneFiveSoftware.Common.Data;
using ZoneFiveSoftware.Common.Data.Fitness;
using ZoneFiveSoftware.Common.Visuals;
using ZoneFiveSoftware.Common.Visuals.Fitness;
using ZoneFiveSoftware.Common.Visuals.Util;
using FilteredStatistics.Controller;

namespace FilteredStatistics.View
{
    class FilteredStatisticsActivityDetailPage : STFrameworkDetailPage
    {
        public FilteredStatisticsActivityDetailPage(IDailyActivityView view)
        {
            PluginMain.GetApplication().SystemPreferences.PropertyChanged += new PropertyChangedEventHandler(OnSystemPreferencesPropertyChanged);

            m_DailyActivityView = view;

            // Prevent warning, unused parameter
            if (PropertyChanged != null)
            {
            }
        }

        void OnSystemPreferencesPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (m_Control != null &&
                (e.PropertyName == "AnalysisSettings.IncludePaused" ||
                 e.PropertyName == "AnalysisSettings.IncludeStopped"))
            {
                ActivityInfoCache.Instance.ClearAllInfo();
            }
        }

#region STFrameworkDetailPage Members

        public override Guid Id
        {
            get { return GUIDs.FSDetailsPage; }
        }

        public override bool MenuEnabled
        {
            get { return true; }
        }

        public override IList<string> MenuPath
        {
            get { return null; }
        }

        public override bool MenuVisible
        {
            get { return true; }
        }

        public override bool PageMaximized
        {
            get { return true; }
            set {  }
        }

        public override bool HidePage()
        {
            if (m_Control != null)
            {
                m_Control.DetailPageVisible = false;
            }
            Visible = false;

            return true;
        }

        public override string PageName
        {
            get { return PluginView.GetLocalizedString("ActivityPageTitleText"); }
        }

        public override void ShowPage(string bookmark)
        {
            CreatePageControl();

            if (m_Control != null)
            {
                m_Control.DetailPageVisible = true;
            }
            Visible = true;

            RefreshPage();
        }

        public override ZoneFiveSoftware.Common.Visuals.IPageStatus Status
        {
            get { return null; }
        }

        public override void ThemeChanged(ITheme visualTheme)
        {
            if (m_Control != null)
            {
                m_Control.ThemeChanged(visualTheme);
            }
        }

        public override string Title
        {
            get { return PluginView.GetLocalizedString("ActivityPageTitleText"); }
        }

        public override void UICultureChanged(CultureInfo culture)
        {
            if (m_Control != null)
            {
                m_Control.UICultureChanged(culture);
            }
        }

        public override event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        public override ExtendViewDetailPages.Location Location
        {
            get { return ExtendViewDetailPages.Location.LeftDetail; }
        }

        public override System.Guid MainPluginId
        {
            get { return GUIDs.PluginMain; }
        }

        public override Control DetailPageControl
        {
            get
            {
                if (m_Control == null)
                {
                    m_Control = new FilteredStatisticsControl(m_DailyActivityView);
                    m_Control.DetailPageVisible = Visible;
                }

                return m_Control;
            }
        }

#endregion

        public void RefreshPage()
        {
            if (m_Control != null && m_Control.DetailPageVisible)
            {
                if (m_NeedsRefresh)
                {
                    m_NeedsRefresh = false;
                }
            }
        }

        public IDailyActivityView DailyActivityView
        {
            get { return m_DailyActivityView; }
        }

        public bool Visible
        {
            get { return m_Visible; }
            set { m_Visible = value; }
        }

        private FilteredStatisticsControl m_Control = null;
        private IDailyActivityView m_DailyActivityView = null;
        private bool m_NeedsRefresh = false;
        private bool m_Visible = false;
    }
}
